package hang.study.uistudy;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.Poi;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;

import java.util.List;

/**
 * Created by Administrator on 2015/8/11.
 */
public class BaiduMapActivity extends Activity {
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    MapView mMapView = null;
    BaiduMap mBaiduMap = null;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        //百度地图初始化
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.baidu_map_main);
        mMapView = (MapView)findViewById(R.id.bmapView);

        //百度定位初始化
        mLocationClient = new LocationClient(getApplicationContext());
        mLocationClient.registerLocationListener(myListener);
        initLocation();
        mLocationClient.start();

        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);

    }

    @Override
    protected void onPause(){
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onResume(){
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        mMapView.onDestroy();
    }

    /**
     * 配置定位参数
     */
    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        //可选，默认高精度，设置定位模式：高精度，低功耗，仅设备
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选，默认gcj02，设置返回的定位结果坐标系
        option.setCoorType("bd0911");
        //定位时间间隔,最少1000，默认是0
        option.setScanSpan(1000);
        //是否需要地址信息
        option.setIsNeedAddress(true);
        //是否使用GPS
        option.setOpenGps(true);
        //当GPS有效时，是否按照1秒1次频率输出GPS
        option.setLocationNotify(true);
        //是否需要附近描述，BDLocation.getLocationDescripe里得到如“天安门附近”
        option.setIsNeedLocationDescribe(false);
        //是否需要POI结果，BDLocation.getPoiList得到
        option.setIsNeedLocationPoiList(true);
        //是否在stop的时候杀死定位进程，定位SDK是放在一个SERVICE内
        option.setIgnoreKillProcess(false);
        //默认false
        option.SetIgnoreCacheException(false);
        //
        option.setEnableSimulateGps(true);
        //
        mLocationClient.setLocOption(option);
    }

    public class MyLocationListener implements BDLocationListener{
        @Override
        public void onReceiveLocation(BDLocation location){
            //Receive Location
            StringBuilder sb = new StringBuilder(256);
            sb.append("time: ");
            sb.append(location.getTime());
            sb.append("\nerror code: ");
            sb.append(location.getLocType());
            sb.append("\nlatitude :");
            sb.append(location.getLatitude());
            sb.append("\nradius :");
            sb.append(location.getRadius());

            if(location.getLocType() == BDLocation.TypeGpsLocation){
                //GPS 定位结果
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());//每小时几公里
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());//卫星数量
                sb.append("\nheight : ");
                sb.append(location.getAltitude());//高度
                sb.append("\ndirection : ");
                sb.append(location.getDirection());//方向
                sb.append("\naddr");
                sb.append(location.getAddrStr());
                sb.append("\ndescribe");
                sb.append("gps定位成功");
            }else if(location.getLocType() == BDLocation.TypeNetWorkLocation){
                //网络定位结果
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                //运营商
                sb.append("\noperators : ");
                sb.append(location.getOperators());
                sb.append("\ndescribe : ");
                sb.append("网络定位成功");
            }else if(location.getLocType() == BDLocation.TypeOffLineLocation){
                //离线定位结果
                sb.append("\ndescribe : ");
                sb.append("离线定位成功，离线定位结果也是有效的");
            }else if(location.getLocType() == BDLocation.TypeServerError){
                //
                sb.append("\ndescribe : ");
                sb.append("服务端网络定位失败。");
            }else if(location.getLocType() == BDLocation.TypeNetWorkException){
                //
                sb.append("\ndescribe ; ");
                sb.append("网络不同导致定位失败，请检查网络是否通畅");
            }else if(location.getLocType() == BDLocation.TypeCriteriaException){
                //
                sb.append("\ndescribe : ");
                sb.append("无法获取有效定位导致定位失败，可能是飞行模式原因");
            }

            sb.append("\nlocation describe : ");
            sb.append(location.getLocationDescribe());
            List<Poi> list = location.getPoiList();
            if(list != null){
                sb.append("\n poi list size = : ");
                sb.append(list.size());
                for(Poi p : list){
                    sb.append("\npoi= : ");
                    sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
                }
            }

            Log.i("BaiduLocationApiDem",sb.toString());
        }
    }

}
